<template>
  <div class="table-sty box-shadow">
    <div class="tabpane topcolor flexb othertop">
      <a v-for="item in menu"
        :key="item.value"
        @click="handleMenuActive(item.value)"
        :class="{ 'active': item.value === active }"
        class="flex">
        <span>
          {{$t('cpuNet.leaseOrders')}}
        </span>
      </a>
      <!-- 续租失败 -->
      <div class="pr" v-if="scatter && scatter.identity && scatter.chain==='eos'">
        <!-- <el-checkbox v-model="hidOtherKey">{{$t('jy.ycother')}}</el-checkbox> -->
        <!-- <a class="shuaxin" v-if="scatter && scatter.identity"
        :class="{'act' : refresh}" @click="handleRefresh"><i class="el-icon-refresh"></i></a> -->
        <span @click="failVisible = true;" style="cursor: pointer;">{{$t('cpuNet.rentRenewalFailed')}}?</span>
      </div>
    </div>
    <div class="trbtable pt">
      <el-table
        class="outTable"
        v-loading="loading"
        :data="tableData"
        :empty-text="scatter && !scatter.identity ? $t('jy.sqhlook') : $t('jy.noData')"
        style="width: 100%">
        <!-- 方向 -->
        <!-- <el-table-column :label="$t('order.direction')" width="50">
          <template slot-scope="props">
            <span v-if="props.row.direction === 1" class="color-green">{{$t('order.buy')}}</span>
            <span v-if="props.row.direction === 2" class="color-red">{{$t('order.sell')}}</span>
          </template>
        </el-table-column> -->
        <!-- 类型 -->
        <el-table-column :label="$t('newpos.type')" width="100">
          <template slot-scope="props">
            <span>{{props.row.type.toUpperCase()}}</span>
          </template>
        </el-table-column>
        <!-- 接收账户 -->
        <el-table-column :label="$t('newpos.receiveAccount')" width="110">
          <template slot-scope="props">
            <a class="colorThis" :href="`https://bloks.io/account/${props.row.receiver}`" target="_blank">{{props.row.receiver}}</a>
          </template>
        </el-table-column>
        <!-- 起租时间 -->
        <el-table-column :label="$t('cpuNet.startingTime')" width="145">
          <template slot-scope="props">
            {{props.row.beginTime | dateFormatTYear}}
          </template>
        </el-table-column>
        <!-- 到期时间 -->
        <el-table-column :label="$t('cpuNet.maturityTime')" width="145">
          <template slot-scope="props">
            {{props.row.expiration | dateFormatTYear}}
          </template>
        </el-table-column>
        <!-- 租金 -->
        <el-table-column :label="$t('cpuNet.cpuNetRent')" width="125">
          <template slot-scope="props">
            {{props.row.payment}}
          </template>
        </el-table-column>
        <!-- 租借数量 -->
        <el-table-column :label="$t('cpuNet.rentalQuantity')">
          <template slot-scope="props">
            {{props.row.total_staked}} ({{ props.row.type.toUpperCase() }})
          </template>
        </el-table-column>
        <!-- 自动续租 -->
        <el-table-column :label="$t('cpuNet.AutoPay')" width="100">
          <template slot-scope="props">
            <span v-if="props.row.renewal" class="color-lv">{{ $t('cpuNet.cpuNetYes') }}</span>
            <span v-else>{{ $t('cpuNet.cpuNetNo') }}</span>
          </template>
        </el-table-column>
        <!-- 续租租金 -->
        <el-table-column :label="$t('cpuNet.rentRenewal')" width="125">
          <template slot-scope="props">
            {{props.row.balance}}
          </template>
        </el-table-column>
        <!-- 续租期限 -->
        <el-table-column :label="$t('cpuNet.rentRenewalPeriod')" width="100">
          <template slot-scope="props">
            <span>
              {{props.row.renewalTerm}}<span class="fontx wordWrap" style="color: #999">{{ $t('cpuNet.day') }}</span>
            </span>
            <span class="colorThis" @click="handleAdminAction(props.row)">{{ $t('cpuNet.Administration') }}</span>
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column :label="$t('order.avgType2')" width="70">
          <template slot-scope="props">
            <span class="color-lv">{{ $t('cpuNet.leasing') }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 管理续租费用弹窗 -->
    <el-dialog
      class="admin"
      :close-on-click-modal="false"
      :visible.sync="adminVisible"
      width="500px">
      <admin
      :adminItem="adminItem"
      :eosBalance="eosBalance"
      :fundBalance="fundBalance"
      :adminVisible="adminVisible"
      :symbolScience="symbolScience"
      @listenAdmin="handleAdmin"/>
    </el-dialog>
    <!-- 续租失败 -->
    <el-dialog
      class="failDialog"
      :title="$t('cpuNet.rentRenewalFailed')"
      center
      :close-on-click-modal="false"
      :visible.sync="failVisible"
      width="500px">
      <fail :failVisible="failVisible"
      :scatter="scatter"
      :fundBalance="fundBalance"
      :symbolScience="symbolScience"
      @listenFail="handleFail"/>
    </el-dialog>
  </div>

</template>

<script>
import moment from 'moment';
import { EosModel } from '@/utils/eos';
import { toFixed } from '@/utils/validate';
import Admin from './Admin';
import Fail from './Fail';

export default {
  name: 'trading-bottom-history',
  data() {
    return {
      tableData: [],
      adminVisible: false, // 管理页面弹窗
      failVisible: false, // 续租失败弹窗
      loading: false,
      refresh: false,
      active: 0,
      menu: [
        {
          name: 'jyHistory',
          value: 0
        }
      ],
      adminItem: {},
    };
  },
  components: {
    Admin,
    Fail
  },
  props: {
    scatter: Object,
    refreshOrder: Boolean,
    eosBalance: String,
    symbolScience: String,
    fundBalance: Number, // 储备金余额
  },
  watch: {
    'scatter.identity': function change(val) {
      if (val) {
        this.handleGetUserOrder();
        return;
      }
      this.tableData = [];
    },
    refreshOrder: function change() {
      this.handleRefresh();
    },
  },
  created() {
  },
  mounted() {
    this.handleGetUserOrder();
  },
  methods: {
    // 关闭续租管理回调
    handleAdmin(key) {
      if (key === 'refresh') {
        this.handleRefresh();
        this.adminVisible = false;
      }
    },
    // 关闭续租失败回调
    handleFail() {
      this.failVisible = false;
    },
    // 数字截取 - 保留小数
    handleToFixed(num, p) {
      return toFixed(num, p);
    },

    // 获取当前委托订单列表
    handleGetUserOrder() {
      this.tableData = [];
      if (this.scatter && (!this.scatter.identity || this.scatter.chain !== 'eos')) return;
      this.loading = true;
      const params = {
        table: 'cpuloan', // "cpuloan"  "netloan"
      }
      EosModel.getCpuNetList(params, (resCpu) => {
        params.table = 'netloan';
        EosModel.getCpuNetList(params, (resNet) => {
          this.loading = false;
          this.refresh = false;
          let cpuList = [];
          let netList = [];
          if (resCpu && resCpu.rows && resCpu.rows.length > 0) {
            cpuList = resCpu.rows;
            cpuList.forEach((v) => {
              const vv = v;
              vv.type = 'cpu'
              vv.direction = 1
              vv.beginTime = moment.utc(vv.expiration).subtract(30, 'd');
              vv.renewal = parseFloat(v.balance, 0) - parseFloat(v.payment, 0) >= 0;// 是否续租
              vv.renewalTerm = Math.floor(parseFloat(v.balance, 0) / parseFloat(v.payment, 0)) * 30;// 续租期限
            });
          }
          if (resNet && resNet.rows && resNet.rows.length > 0) {
            netList = resNet.rows;
            netList.forEach((v) => {
              const vv = v;
              vv.type = 'net'
              vv.direction = 1
              vv.beginTime = moment.utc(vv.expiration).subtract(30, 'd');
              vv.renewal = parseFloat(v.balance, 0) - parseFloat(v.payment, 0) >= 0;// 是否续租
              vv.renewalTerm = Math.floor(parseFloat(v.balance, 0) / parseFloat(v.payment, 0)) * 30;// 续租期限
            });
          }
          cpuList.push(...netList);
          this.tableData = cpuList;
          if (this.tableData.length) {
            this.tableData.sort((a, b) => b.loan_num - a.loan_num);
          }
        });
      });
    },
    handleAdminAction(item) {
      this.adminItem = item;
      this.adminVisible = true;
    },
    handleRefresh() {
      if (this.refresh) return;
      this.refresh = true;
      setTimeout(() => {
        this.handleGetUserOrder();
      }, 500);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/variables.scss";
$-color-lv: #67C269;
$-color-ho: #EE1443;
$-clolor: #278eda;
.color-lv {
  color: $-color-lv;
}
.color-ho {
  color: $-color-ho;
}
.nodataj {
  display: inline-block;
}
.table-sty {
  margin-top: 10px;
}

.topcolor {
  background: $-topcolorf7;
}

.trbtable {
  background: #fff;
  padding: 0 25px;
}

.colorThis {
  cursor: pointer;
}

.cozyTipContent {
  width: 240px;
  font-size: 12px;
  line-height: 25px;
  padding: 5px;
  font-family: SourceHanSansSC-regular;
  color: #999;
}

.cozyTip {
  display: inline-block;
  i {
    font-weight: bold;
    font-size: 14px;
    vertical-align: middle;
  }
}

.shuaxin {
  display: inline-block;
  margin: 0;
  font-size: 14px;
  padding: 0px 5px;

  &.act i{
    animation: rotating 1s linear infinite;
  }
}

.wordWrap{
  word-wrap: break-word;
  word-break: normal;
}

.failDialog {
  /deep/ .el-dialog__header {
    padding-top: 25px;
  }
}

.theme-1B1D27 {
  .topcolor {
    background: $-topcolor2;
  }

  .trbtable {
    background: $-black1;
    /deep/ .cell {
      color: rgba(255, 255, 255, 0.6);
    }
  }
}
.tabpane /deep/.el-checkbox__label {
  color: #666;
  font-size: 12px;
}
.table-sty /deep/.el-table {
  .priceTip {
    line-height: 14px;
    margin-bottom: -4px;
    padding: 0;
    .priceTipIcon {
      line-height: 14px;
      vertical-align: middle;
      padding: 0;
      margin-left: 3px;
    }
  }
  font-size: 12px;
  .el-table__header-wrapper {
    font-size: 12px;
  }
  .el-table__empty-text {
    font-size: 12px;
    color: #666;
  }
  .cell {
    font-weight: normal;
  }
  .cell span{
    font-size: 12px;
  }
  .el-table__expand-icon {
    visibility: hidden;
  }
  .el-table__body tr .cell,
  .el-table__header tr .cell{
    text-align: center;
  }

  .el-table__body tr td:nth-child(2) .cell,
  .el-table__header tr th:nth-child(2) .cell,
  .el-table__body tr td:nth-child(5) .cell,
  .el-table__header tr th:nth-child(5) .cell,
  .el-table__body tr td:nth-child(6) .cell,
  .el-table__header tr th:nth-child(6) .cell,
  .el-table__body tr td:nth-child(8) .cell,
  .el-table__header tr th:nth-child(8) .cell,
  .el-table__body tr td:nth-child(9) .cell,
  .el-table__header tr th:nth-child(9) .cell {
    text-align: right;
  }
  .el-table__body tr td:nth-child(4) .cell {
    padding-right: 12px;
  }
  .innerTable{
    .el-table__body tr td .cell,
    .el-table__header tr th .cell{
      text-align: right;
    }

    .el-table__body tr td:nth-child(1) .cell,
    .el-table__header tr th:nth-child(1) .cell{
      text-align: right;
    }

    .el-table__body tr td:nth-child(5) .cell,
    .el-table__header tr th:nth-child(5) .cell{
      padding-right: 50px;
    }
  }
}

// @media screen and (max-width: 1280px) {
//   .table-sty {
//     width: 1240px;
//   }
// }
</style>
